package com.mzxy.view;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.activity.Loginctivity;
import com.mzxy.utils.Constants;
import com.mzxy.utils.SpUtiles;

/**
 * Created by Administrator on 2018/4/11.
 */

public class MyInfoView implements View.OnClickListener{
    private View mCurrentView;
    private Activity mContext;
    private LinearLayout llHead;
    private TextView tvUserName;
    private RelativeLayout rlCourseHistory;
    private RelativeLayout rlSetting;
    private RelativeLayout titleBar;
    public  MyInfoView(Activity context){
        mContext = context;
    }
    public  View getView(){
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;
    }
    public void setLoginParam(String username){
        if (TextUtils.isEmpty(username)){
            tvUserName.setText("点击登录");
        }
        else {
            tvUserName.setText(username);
        }
    }

    private void createView() {
        mCurrentView = LayoutInflater.from(mContext).inflate(R.layout.testv3,null);
        llHead = mCurrentView.findViewById(R.id.ll_head);
        rlCourseHistory = mCurrentView.findViewById(R.id.rl_course_history);
        rlSetting = mCurrentView.findViewById(R.id.rl_setting);
        tvUserName = mCurrentView.findViewById(R.id.tv_user_name);
        titleBar = mContext.findViewById(R.id.title_bar);

        llHead.setOnClickListener(this);
        rlCourseHistory.setOnClickListener(this);
        rlSetting.setOnClickListener(this);

        initLogin();
    }

    private void initLogin() {
        if (SpUtiles.readLoginStatus(mContext)){
            setLoginParam(SpUtiles.getLoginUsername(mContext));
        }
    }
    public void showView(){
        if (mCurrentView == null){
            createView();
        }
        titleBar.setVisibility(View.GONE);
        mCurrentView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_head:
                if (!SpUtiles.readLoginStatus(mContext)){
                    mContext.startActivityForResult(new Intent(mContext, Loginctivity.class), Constants.MYINFO2LOGINCTIVITY);
                }
                break;
            case R.id.rl_course_history:
                break;
            case R.id.rl_setting:
                if (!SpUtiles.readLoginStatus(mContext)){
                    Toast.makeText(mContext,"您还未登陆，请先登陆",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(mContext,"跳转到设置界面",Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }


}
